package com.ciwong.mobilepay.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.ciwong.mobilelib.R;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.widget.TitleBar;
import com.ciwong.mobilepay.util.PayUtil;

/**
 * 二维码支付
 * Created by yyf on 2017/7/27.
 */

public class QRCodePayActivity extends Activity {
    TitleBar title_bar;
    ImageView iv_pay_qr;
    RequestQueue requestQueue;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        initView();
        initListener();
        initData();
    }


    private void initView() {

        title_bar = (TitleBar) findViewById(R.id.title_bar);
        title_bar.setBackListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View v) {
//                setResult(RESULT_OK);
                finish();
            }
        });

        iv_pay_qr = (ImageView) findViewById(R.id.iv_pay_qr);
    }

    @Override
    public void onBackPressed() {
//        Intent intent = new Intent();
//        intent.putExtra(EMainActivity.PAY_RESULT, "success");
//        setResult(RESULT_OK,intent);
        finish();
    }

    private void initListener() {
        title_bar.setTitle("支付详情");
        title_bar.setBackListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View v) {
                onBackPressed();
            }
        });
    }

    String code_url;

    private void initData() {
        Intent intent = getIntent();

        code_url = intent.getStringExtra("code_url");
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        loadPayQR();
    }

    /**
     * 加载微信支付二维码
     */
    private void loadPayQR() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int with = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 228, displayMetrics);
        int heigh =(int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 228, displayMetrics);
        Bitmap qRcodeBitmap = PayUtil.createQRcodeBitmap(code_url, with,   heigh);
        iv_pay_qr.setImageBitmap(qRcodeBitmap);
    }

}
